package com.itmiaojie.quartz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itmiaojie.mapper.ArticleMapper;
import com.itmiaojie.pojo.Article;
import com.itmiaojie.utils.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ViewCountJob extends QuartzJobBean {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //每十秒钟将count 更新到mysql中
        List<Long> ids = articleMapper.selectIds();
        for (Long id : ids) {
            String count = redisCache.getCacheObject(id.toString());
            if (count == null){
                redisCache.setCacheObject(id.toString(),String.valueOf(0));
            }
            articleMapper.updateViewCount(id,Long.valueOf(count));
        }
      /*  List<Article> articles = articleMapper.selectList(null);
        for (Article article : articles) {
            String count = redisCache.getCacheObject(article.getId().toString());
            log.info(count);
            article.setViewCount(Long.valueOf(count));
            articleMapper.updateById(article);
        }*/

    }
}
